local I = require "packages/iberia/utility"

local baohe = fk.CreateSkill{
  name = "iberia__baohe",
}

Fk:loadTranslationTable{
  ["iberia__baohe"] = "暴喝",
  [":iberia__baohe"] = "同势力角色的出牌阶段结束时，你可以对所有攻击范围内包含其的异势力角色拼点：没赢且拼点牌不为【杀】的角色弃置一张牌。",
  ["#iberia__baohe"] = "暴喝：可以对所有攻击范围内包含%src的异势力角色拼点" ..
    "<br/>没赢且拼点牌不为【杀】的角色弃置一张牌",
}

baohe:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(baohe.name) and I.isMate(player, target) then
      local tos = {}
      for _, p in ipairs(player.room.alive_players) do
        if p:inMyAttackRange(target) and not p.kingdom == "unknown" and not I.isMate(player, p) then
          table.insert(tos, p)
        end
      end

      event:setCostData(self, { tos = tos })
      return table.find(tos, function(to) return player:canPindian(to) end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = baohe.name,
      prompt = "#iberia__baohe:" .. target.id,
    })
  end,
  on_use = function (self, event, target, player, data)
    local tos = table.filter(event:getCostData(self).tos, function(to) return player:canPindian(to) end)

    if #tos == 0 then return end
    local pindian = I.Pindian(player, tos, baohe.name)
    for p, r in pairs(pindian.results) do
      if not player.dead and pindian.fromCard.trueName ~= "slash" and player ~= r.winner then
        player.room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          skill_name = baohe.name,
          cancelable = false,
          include_equip = true,
        })
      end
      if not p.dead and r.toCard.trueName ~= "slash" and p ~= r.winner then
        p.room:askToDiscard(p, {
          min_num = 1,
          max_num = 1,
          skill_name = baohe.name,
          cancelable = false,
          include_equip = true,
        })
      end
    end
  end
})

return baohe